package users

import (
	"database/sql"
	"log"
	"irisx/models"
	"irisx/db"
)

var dao = db.DB

// 查询所有用户
// 更多用法参考： https://www.jianshu.com/p/06f26f879d61
func QueryUsers() []models.Users{
	log.Print("-->查询所有用户")
	var (
		user models.Users
		users []models.Users
		rows *sql.Rows
		sql string
		err error
	)
	sql = "select * from users"
	if rows,err = dao.Query(sql);err != nil{
		log.Print("查询错误, err = ",err)
		return nil
	}
	for rows.Next(){
		if err = rows.Scan(&user.ID,&user.Name,&user.Age);err != nil{
			log.Print("scan failed")
			return nil
		}
		users = append(users,user)
	}
	return users
}
